package org.linlinjava.litemall.taobaoke.service.impl;

import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkDgOptimusMaterialResponse;
import com.taobao.api.response.TbkJuTqgGetResponse;
import org.linlinjava.litemall.taobaoke.DTO.*;
import org.linlinjava.litemall.taobaoke.sdk.MaterialDao;
import org.linlinjava.litemall.taobaoke.service.MaterialService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author haoting.wang
 * @date 2019-07-26 16:53
 **/
@Service
public class MaterialServiceImpl implements MaterialService {


    @Autowired
    private MaterialDao materialDao;

    @Override
    public List<ItemInfo> query(MaterialQueryDTO materialQueryDTO) {

        List<TbkDgMaterialOptionalResponse.MapData> mapDataList = materialDao.query(materialQueryDTO);

        if(CollectionUtils.isEmpty(mapDataList)){
            return Collections.EMPTY_LIST;
        }
        List<ItemInfo> result = new ArrayList(mapDataList.size());

        for(TbkDgMaterialOptionalResponse.MapData mapData:mapDataList){

            ItemInfo itemInfo = new ItemInfo();
            BeanUtils.copyProperties(mapData, itemInfo);

            result.add(itemInfo);
        }

        return result;

    }

    @Override
    public List<ItemInfo> queryFeatured(MaterialFeaturedQueryDTO materialFeaturedQueryDTO) {

        List<TbkDgOptimusMaterialResponse.MapData> mapDataList = materialDao.queryFeatured(materialFeaturedQueryDTO);

        if(CollectionUtils.isEmpty(mapDataList)){
            return Collections.EMPTY_LIST;
        }
        List<ItemInfo> result = new ArrayList(mapDataList.size());

        for(TbkDgOptimusMaterialResponse.MapData mapData:mapDataList){

            ItemInfo itemInfo = new ItemInfo();
            itemInfo.setItemId(mapData.getItemId());
         //   itemInfo.setCouponInfo(mapData.getCouponInfo());
            itemInfo.setCouponAmount(String.valueOf(mapData.getCouponAmount()));
            itemInfo.setCouponInfo("满"+mapData.getCouponStartFee()+"元减"+mapData.getCouponInfo()+"元");
            itemInfo.setCouponShareUrl(mapData.getCouponShareUrl());
            itemInfo.setItemDescription(mapData.getItemDescription());
            itemInfo.setItemUrl(mapData.getClickUrl());
            itemInfo.setPictUrl(mapData.getPictUrl());
            itemInfo.setReservePrice(mapData.getReservePrice());
            itemInfo.setShortTitle(mapData.getShortTitle());
            itemInfo.setSmallImages(mapData.getSmallImages());
            itemInfo.setTitle(mapData.getTitle());
            itemInfo.setUserType(mapData.getUserType());
            itemInfo.setVolume(mapData.getVolume());
            itemInfo.setReservePrice(mapData.getZkFinalPrice());//有点特殊

            DecimalFormat df = new DecimalFormat("0.00");
            double finalPrice = Double.valueOf(mapData.getZkFinalPrice()) - mapData.getCouponAmount();
            itemInfo.setZkFinalPrice(df.format(finalPrice));

            result.add(itemInfo);
        }

        return result;
    }

    private String parseUrl(String url){

        return url.startsWith("//")?url.substring(2):url;
    }

    @Override
    public List<ItemInfo> queryTqg(TqgQueryDTO tqgQueryDTO) {
        List<TbkJuTqgGetResponse.Results> tqgList = materialDao.queryTqg(tqgQueryDTO);

        if(CollectionUtils.isEmpty(tqgList)){
            return Collections.EMPTY_LIST;
        }
        List<ItemInfo> result = new ArrayList(tqgList.size());

        for(TbkJuTqgGetResponse.Results tqg:tqgList){

            ItemInfo itemInfo = new ItemInfo();

            BeanUtils.copyProperties(tqg, itemInfo);

            result.add(itemInfo);
        }

        return result;
    }
}
